import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
public class happy_number {
    public static void main (String[] args) throws IOException{
        if (args.length<1) throw new RuntimeException ("not enough arguments");
        if (args.length>1) throw new RuntimeException ("too many arguments");
        BufferedReader reader = new BufferedReader (new FileReader (new File (args[0])));
        String line;
        while ((line = reader.readLine()) != null){
           boolean happy = false; 
           int num = Integer.parseInt(line);
           for (int i = 0;i<100;i++){ 
               if (num == 1) {
                   System.out.println("1");
                   happy = true;
                   break;
               }
               num = algo(num);
           }
           if (!happy) System.out.println("0");  
        }
    }
    
    public static int algo (int num){
        int total = 0;
        int digit;
        String number = Integer.toString(num);
        for (int j = 0;j<number.length();j++){
            digit = Character.getNumericValue(number.charAt(j));
            digit = digit*digit; //take the square    
            total = total + digit; // add to the total number
        }
        return total;
    }
}
